<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="关键字" prop="search">
        <el-input v-model="queryParams.search" placeholder="标题" clearable style="width: 100%" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="类型" prop="newsType">
        <el-select v-model="queryParams.newsType" placeholder="类型">
          <el-option v-for="dict in newsTypeList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 100%">
          <el-option v-for="dict in this.informationStatusList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="发布时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['basics:information:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table :height="tableHeight" v-loading="loading" :data="informationList" stripe border>
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="类型" prop="newsType" width="120">
        <template slot-scope="scope">
          {{getLabel(newsTypeEnum,scope.row.newsType)}}
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="title" width="200" :show-overflow-tooltip="true" />
      <el-table-column label="状态" prop="status" width="150" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basics_information_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="缩略图" width="250">
        <template slot-scope="scope">
          <table-image :imgDicUrls="scope.row.imgList"></table-image>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" prop="publishTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="简介" prop="intro" class-name="fixed-width" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['basics:information:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <Z-Dialog :title="title" :visible.sync="open" @confirm="submitForm">
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item label="类型" prop="newsType">
          <el-select v-model="form.newsType" placeholder="类型选择" clearable style="width: 100%">
            <el-option v-for="dict in newsTypeEnum" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="状态选择" clearable style="width: 100%">
            <el-option v-for="dict in dict.type.basics_information_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="缩略图" prop="imgList">
          <image-upload :valueData="form.imgList" params="imgList" @uploaded="uploaded"></image-upload>
        </el-form-item>
        <el-form-item label="MP4视频" prop="videoList">
          <image-upload :valueData="form.videoList" params="videoList" @uploaded="uploadedMP4"></image-upload>
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker v-model="form.publishTime" style="width:100%" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss" />
        </el-form-item>
        <el-form-item label="简介" prop="intro">
          <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="form.intro" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <editor :value="form.content" :height="400" ref="content"></editor>
        </el-form-item>
      </el-form>
    </Z-Dialog>
  </div>
</template>

<script>
import tableImage from "@/components/TableImageView/index.vue";
import { listBasenews, addBasenews, editBasenews, listNewTypes } from "@/api/basics/information.js";
// import { newsTypeEnum } from '@/common/enums'
import { getLabelByValue } from '@/common/common'

export default {
  components: { tableImage },
  name: "information",
  dicts: ["basics_information_status"],
  data() {
    return {
      newsTypeEnum: [],
      //上传的图片
      dialogImageUrl: "",
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息发布列表数据
      informationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 日期范围
      dateRange: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,

      },
      pageNum: 1,
      // 表单参数
      form: {},

      rules: {
        title: [{ required: true, message: "必填", trigger: "blur" }],
        status: [{ required: true, message: "必填", trigger: "blur" }],
        content: [{ required: true, message: "必填", trigger: "blur" }],
        intro: [{ required: true, message: "必填", trigger: "blur" }],
        newsType: [{ required: true, message: "必填", trigger: "blur" }],
        publishTime: [{ required: true, message: "必填", trigger: "blur" }],
      }
    };
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    },
    newsTypeList() {
      let typeDictArray = JSON.parse(
        JSON.stringify(this.newsTypeEnum)
      );
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
    informationStatusList() {
      let typeDictArray = JSON.parse(
        JSON.stringify(this.dict.type.basics_information_status)
      );
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
  },
  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {

        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
    dateRange(newV, oldV) {
      if (newV.length >= 2) {
        this.queryParams.buyStartTime = String(newV[0]) + ' ' + '00:00:00';
        this.queryParams.buyEndTime = String(newV[1]) + ' ' + '23:59:59';
        this.getList();
      }
    },
  },
  created() {
    this.listNewTypes();

    this.getList();
  },
  methods: {
    listNewTypes() {
      listNewTypes(this.queryParams)
        .then((response) => {
          if (response.code == 200) {
            this.newsTypeEnum = response.data;

            this.newsTypeEnum.forEach(item => {
              item.label = item.text;
              item.value = item.value
            });
          }
        })

    },
    getList() {
      this.loading = true;
      listBasenews(this.queryParams)
        .then((response) => {
          if (response.code == 200) {
            this.informationList = response.data;
            this.total = response.total;
          }
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    getLabel(arr, value) {
      return getLabelByValue(arr, value);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.resetForm("form");
      this.form = {};
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "信息发布";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "信息编辑";
      this.form = JSON.parse(JSON.stringify(row));
      this.form.status = String(row.status);
    },

    /** 提交按钮 */
    submitForm: function () {
      //富文本数据
      let editHtml = this.$refs.content.currentValue;
      this.form.content = editHtml;
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            editBasenews(this.form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBasenews(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /**上传视频 */
    uploadedMP4(params, dict) {
      this.form[params] = JSON.stringify(dict);
    },

    /**上传数据 */
    uploaded(params, dict) {
      this.form[params] = JSON.stringify(dict);
    },
  },
};
</script>
<style scoped>
</style>
